package com.fss.activity.dao;
import com.fssDataModels.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ActivityDaoImpl implements ActivityDao {
	private SessionFactory _sessions;
	
	public ActivityDaoImpl()
	{
		// Create the SessionFactory from hibernate.cfg.xml
		_sessions = new Configuration()				
				.configure()
				.buildSessionFactory();
	}
	//CRUD functions
	public TbActivity getActivity(Integer activityId)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		TbActivity result = null;
		try {
			tx = session.beginTransaction();
			String hql=	"select activity " +
						"from TbActivity as activity " +
						"where activity.activityId=:activityId ";		
			
			Query query = session.createQuery(hql);
			query.setParameter("activityId", activityId);
			
			if(!query.list().isEmpty())				
				result=(TbActivity)query.list().get(0);
			
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			//do nothing else
		}
		finally {
			session.close();
		}
		return result;
		
	}
	
	//return: - 0 --> failed
	//		  - 1 --> success
	public int insertActivity(TbActivity activity)
	{
	
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.persist(activity);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	//return: - 0 --> failed
	//		  - 1 --> success
	public int updateActivity(TbActivity activity)
	{
		Session session = _sessions.openSession();
		Transaction tx = null;
		int result=1;
		try {
			tx = session.beginTransaction();
			session.update(activity);
			tx.commit();
		}
		catch (HibernateException he) {
			if (tx!=null) tx.rollback();
			result=0;
		}
		finally {
			session.close();
		}
		
		return result;
	}
	
	//function: deleteActivity
	//return: - 0 --> not deleted
	//		  - 1 --> deleted
	public int deleteActivity(TbActivity activity)
	{
		int success=0;
		Session session = _sessions.openSession();
		Integer activityId= activity.getActivityId();
		try {		    
		      String hql = "delete from TbActivity activity where activity.activityId = :activityId";
		      Query query = session.createQuery(hql);
		      query.setParameter("activityId", activityId);
		      int row = query.executeUpdate();
		      if (row == 0){
		        success=1;//delete command not successfully executed
		      }
		      else{
		        success=1;//delete command successfully executed
		      }
		    }
		    catch(HibernateException he){
		    	//do nothing
		    	success=0;//delete command not successfully executed
		    }
		    finally {
				session.close();
			}
		return success;
	}
}
